# Maintainer: Biswapriyo Nath <nathbappai@gmail.com>

_realname=libmediainfo
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=25.04
pkgrel=1
pkgdesc="Supplies technical and tag information about media files (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="https://mediaarea.net"
msys2_repository_url='https://github.com/MediaArea/MediaInfoLib'
license=("spdx:BSD-2-Clause")
makedepends=(
  "${MINGW_PACKAGE_PREFIX}-cc"
  "${MINGW_PACKAGE_PREFIX}-cmake"
  "${MINGW_PACKAGE_PREFIX}-ninja"
)
depends=(
  "${MINGW_PACKAGE_PREFIX}-gcc-libs"
  "${MINGW_PACKAGE_PREFIX}-curl"
  "${MINGW_PACKAGE_PREFIX}-glib2"
  "${MINGW_PACKAGE_PREFIX}-libzen"
  "${MINGW_PACKAGE_PREFIX}-tinyxml2"
  "${MINGW_PACKAGE_PREFIX}-graphviz"
  "${MINGW_PACKAGE_PREFIX}-zlib"
)

source=("https://mediaarea.net/download/source/${_realname}/${pkgver}/${_realname}_${pkgver}.tar.bz2"
        001-fix-win32-build.patch
        002-dllname.patch
        003-libmedainfo-reader-allow-shared-curl-linking.patch)
sha256sums=('a5c5ce1e21d40c6907c47a9459c3b5f36cd5c7a0e5800f87419da10b9267becd'
            'e5ff9f586b07c638bfea79055543c5f9b24f88b74ccba38f3176b432ba55e30e'
            'c13a7ad987431c518233566592408b2f26d00622a901f893ba066023884512dd'
            '7f1e47c682d502600621d58acd857af526a428609db64c4d4ac8883d07f858e6')

apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying $_patch"
    patch -Nbp1 -i "${srcdir}/$_patch"
  done
}

prepare() {
  cd "${srcdir}/MediaInfoLib"

  apply_patch_with_msg \
    001-fix-win32-build.patch \
    002-dllname.patch \
    003-libmedainfo-reader-allow-shared-curl-linking.patch
}

build() {
  mkdir -p "${srcdir}/build-${MSYSTEM}" && cd "${srcdir}/build-${MSYSTEM}"

  declare -a _extra_config
  if check_option "debug" "n"; then
    _extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    _extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake \
    -DCMAKE_INSTALL_PREFIX="${MINGW_PREFIX}" \
    -GNinja \
    "${_extra_config[@]}" \
    -DBUILD_ZENLIB="OFF" \
    -DBUILD_ZLIB="OFF" \
    -DBUILD_SHARED_LIBS="ON" \
    -DBIN_INSTALL_DIR="bin" \
    -DLIB_INSTALL_DIR="lib" \
    -DINCLUDE_INSTALL_DIR:PATH="include" \
    -DCMAKE_DLL_NAME_WITH_SOVERSION=ON \
    ../MediaInfoLib/Project/CMake

  ${MINGW_PREFIX}/bin/cmake --build .
}

package() {
  cd "${srcdir}/build-${MSYSTEM}"

  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake.exe --install .

  local PREFIX_WIN=$(cygpath -wm ${MINGW_PREFIX})
  for _f in "${pkgdir}${MINGW_PREFIX}"/lib/cmake/mediainfolib/*.cmake; do
    sed -e "s|${PREFIX_WIN}|\$\{_IMPORT_PREFIX\}|g" -i ${_f}
    sed -e "s|${MINGW_PREFIX}|\$\{_IMPORT_PREFIX\}|g" -i ${_f}
  done

  install -vDm 644 "${srcdir}/MediaInfoLib/LICENSE" -t "${pkgdir}${MINGW_PREFIX}/share/licenses/$_realname/"
  install -vDm 644 "${srcdir}/MediaInfoLib/"{History_DLL.txt,README.md} -t "${pkgdir}${MINGW_PREFIX}/share/doc/$_realname/"
}
